import { TOGGLE_APP } from '@/store/mutation-types'
import store from '@/store'
import { GET } from '@/api/api'
import generateAsyncRoutes from '@/router/generateAsyncRoutes'
import router from '@/router'
export function toggleApp(app) {
  const promise = new Promise((resolve, reject) => {
    GET('/menu/mine/list?app=' + app)
      .then((res) => {
        const obj = {}
        obj[app] = res.data
        generateAsyncRoutes(router, obj)
        store.commit(TOGGLE_APP, app)
        resolve(app)
      })
      .catch((e) => {
        store.commit(TOGGLE_APP, 'portal')
        reject(e)
      })
  })
  return promise
}
export function getAppByPath(path) {
  let app = path.substr(1)
  if (app.indexOf('/') > 0) {
    app = app.substr(0, app.indexOf('/'))
  }
  return app
}
